//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Modules.Dashboard.Components;
using DotNetNuke.Security;
using DotNetNuke.Services.Installer;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Dashboard
{
	partial class Dashboard : PortalModuleBase, IActionable
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			DotNetNuke.Framework.jQuery.RequestRegistration();
			string dashboardJs = ResolveUrl("~/resources/dashboard/jquery.dashboard.js");
			Page.ClientScript.RegisterClientScriptInclude("DashboardJS", dashboardJs);
			ClientAPI.RegisterClientVariable(Page, "dashboardBaseUrl", ControlPath, false);
			ClientAPI.RegisterClientVariable(Page, "appBaseUrl", DotNetNuke.Common.Globals.ApplicationPath, false);
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!UserInfo.IsSuperUser)
			{
				Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Access Denied"), true);
			}

			rptControls.ItemDataBound += new RepeaterItemEventHandler(rptControls_ItemDataBound);

			List<DashboardControl> controls = DashboardController.GetDashboardControls(true);
			rptTabs.DataSource = controls;
			rptTabs.DataBind();
			rptControls.DataSource = controls;
			rptControls.DataBind();
		}
		protected void rptControls_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				DashboardControl control = e.Item.DataItem as DashboardControl;
				if (control != null)
				{
					Control dashboardControl;
					if (control.DashboardControlSrc.ToLowerInvariant().EndsWith("ascx"))
					{
						dashboardControl = LoadControl("~/" + control.DashboardControlSrc);
					}
					else
					{
						dashboardControl = LoadControl(Framework.Reflection.CreateType(control.DashboardControlSrc), null);
					}
					dashboardControl.ID = Path.GetFileNameWithoutExtension(control.DashboardControlSrc);
					PlaceHolder placeHolder = (PlaceHolder)e.Item.FindControl("phControl");
					placeHolder.Controls.Add(dashboardControl);
				}
			}
		}
		public Entities.Modules.Actions.ModuleActionCollection ModuleActions
		{
			get
			{
				ModuleActionCollection actions = new ModuleActionCollection();
				actions.Add(GetNextActionID(), Localization.GetString("Install.Action", LocalResourceFile), ModuleActionType.AddContent, "", "add.gif", Util.InstallURL(TabId, "DashboardControl"), false, SecurityAccessLevel.Host, true, false);
				actions.Add(GetNextActionID(), Localization.GetString("Manage.Action", LocalResourceFile), ModuleActionType.AddContent, "", "icon_profile_16px.gif", EditUrl("DashboardControls"), false, SecurityAccessLevel.Host, true, false);
				actions.Add(GetNextActionID(), Localization.GetString("Export.Action", LocalResourceFile), ModuleActionType.AddContent, "", "lt.gif", EditUrl("Export"), false, SecurityAccessLevel.Host, true, false);
				return actions;
			}
		}
	}
}
